import requests
from baseTools.myLogger import logger

class MyRequests():

    def myRequest(self, method, url, use_token=False, **kwargs):
        """
        实现请求
        :return:
        """
        logger.info("请求接口，请求方式：{}， 请求url：{}， 信息{}".format(method, url, kwargs))
        if use_token:
            url = url + '?access_token={}'.format(self.token)
            logger.info(url)
        self.rep = requests.request(method=method, url=url, **kwargs)

    def getToken(self, method, url, **kwargs):
        self.myRequest(method=method, url=url, **kwargs)
        self.token = self.rep.json().get('access_token')
        logger.info("token获取成功：{}".format(self.token))

    def getCode(self):
        logger.info("获取接口响应码")
        return self.rep.status_code

    def getJson(self):
        logger.info("获取json格式响应")
        return self.rep.json()

    def getText(self):
        logger.info("获取文本格式响应")
        return self.rep.text

# req = MyRequests()
# req.myRequest(method='get', url='https://www.baidu.com')
# print(req.getCode())
# # print(req.getJson())
# print(req.getText())